package com.msconfig.mvp.view.adapter;

/*
 * author: caojunhao
 * date  : 2020/5/11
 * desc  :
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.OperatorBean;
import com.msconfig.event.EditOperateInfoEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpActivity;
import com.msconfig.mvp.view.fragment.SetupGprsAddOperatorFragment;
import com.msconfig.mvp.view.fragment.SetupGprsOperatorManagerFragment;
import com.msconfig.utils.DataCacheUtils;
import com.msconfig.widget.SlideView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class OperatorAdapter extends BaseAdapter {
    private Context mContext;
    private SetupGprsOperatorManagerFragment mFragment;
    private ArrayList<OperatorBean> mList;
    private onSlideViewContentClickListener onContentClick;

    public OperatorAdapter(Context context, SetupGprsOperatorManagerFragment fragment, ArrayList<OperatorBean> list){
       mContext= context;
       mFragment=fragment;
       mList=list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        int id = 0;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.layout_slide_view,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.icon=(ImageView) convertView.findViewById(R.id.iv_icon);
            viewHolder.operator=(TextView) convertView.findViewById(R.id.operator);
            viewHolder.name=(TextView) convertView.findViewById(R.id.name);
            viewHolder.user=(TextView) convertView.findViewById(R.id.user);
            viewHolder.pass=(TextView) convertView.findViewById(R.id.pass);

            viewHolder.svParent=(SlideView) convertView.findViewById(R.id.sv_parent);
            viewHolder.tvEdit=(TextView) convertView.findViewById(R.id.tv_edit);
            viewHolder.tvDel =(TextView) convertView.findViewById(R.id.tv_del);
            viewHolder.rlContent =(RelativeLayout) convertView.findViewById(R.id.content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.operator.setText(mList.get(position).getOperator());
        viewHolder.name.setText(mList.get(position).getName());
        viewHolder.user.setText(mList.get(position).getUser());
        viewHolder.pass.setText(mList.get(position).getPass());
        if(mList.get(position).getOperator().equals(mContext.getString(R.string.china_mobile))){
            id=R.drawable.icon_china_mobile;
        }else if(mList.get(position).getOperator().equals(mContext.getString(R.string.china_unicom))){
            id=R.drawable.icon_china_unicom;
        }else if(mList.get(position).getOperator().equals(mContext.getString(R.string.china_telecom))){
            id=R.drawable.icon_china_telecom;
        }else{
            id=R.drawable.icon_other;
        }
        viewHolder.icon.setImageResource(id);

        viewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((BaseMvpActivity)mContext).goToNextFragment(mFragment, SetupGprsAddOperatorFragment.newInstance());
                EventBus.getDefault().postSticky(new EditOperateInfoEvent(true,position,mList.get(position).getOperator(),
                        mList.get(position).getName(),mList.get(position).getUser(),mList.get(position).getPass()));
                viewHolder.svParent.closeMenu();

            }
        });

        viewHolder.tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //删掉本行，关闭菜单按钮
                viewHolder.svParent.closeMenu();
                mList.remove(mList.get(position));
                notifyDataSetChanged();
                //重新保存当前运营商到文件中方便下次直接加载
                DataCacheUtils.saveListCache(mContext,mList,"already_add_operator");
            }
        });

        viewHolder.rlContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onContentClick.onContentClick(position);
            }
        });

        return convertView;
    }

    public class ViewHolder{

        ImageView icon;
        TextView operator;
        TextView name;
        TextView user;
        TextView pass;

        SlideView svParent;
        TextView tvEdit;
        TextView tvDel;

        RelativeLayout rlContent;

    }

    public interface onSlideViewContentClickListener {
        void onContentClick(int position);
    }

    public void setOnContentClick(onSlideViewContentClickListener onContentClick ){
        this.onContentClick=onContentClick;
    }
}


